import { useStoreData } from "@/stores/data";
import mars from "@/config/mars3d";
// import global from "@/utils/global.data.params";
// import * as graphicStyle from "@/config/property.config";
import { useStore } from "@/stores/index1";
const store = useStore();
const storeData = useStoreData();
import storages from "../storage";
import { saveDrawData } from "@/api/draw";
// import { storeToRefs } from "pinia";
// const { coverageMap } = storeToRefs(storeData);
// const coverageData = storeData.coverageMap;
import renderer from "./renderer";
/**
 * @function setDataMap 写入绘制数据到本地map集合
 * @param
 * @author scw 2024-1-31 14:39:50
 */
export function setDataMap() {}
/**
 * @function monitor 监听图层绘制及绘制过程和结束
 * @param graphicLayer 图层对象
 * @author scw 2024-1-31 15:02:18
 */
export function monitor(graphicLayer: any) {
    graphicLayer
        .on(mars.mars3d.EventType.drawCreated, (e: any) => {
            // console.log("创建完成", e);
            let objData = e.graphic;
            let postData = objData.toJSON();
            postData.style = JSON.stringify(postData.style);
            if (postData.position) {
                postData.positions = JSON.stringify(postData.position);
                postData.position = JSON.stringify(postData.position);
            } else {
                postData.positions = JSON.stringify(postData.positions);
            }
            postData.isShow = "1";

            saveDrawData(postData).then((res: any) => {
                // console.log(res);
                renderer([res.data]);
                // console.log(postData);
                setTimeout(() => {
                    let graphic = store.graphicLayer.getGraphicById(postData.id);
                    store.graphicLayer.removeGraphic(graphic);
                }, 300);
                return;
                // 修改其id
                objData.id = res.data.id;
                // 添加树菜单数据
                let treeData = {
                    editor: true,
                    id: res.data.id,
                    isLocked: 0,
                    name: res.data.name,
                    pid: res.data.folderId,
                    show: 1,
                    sort: 0,
                    type: res.data.type,
                    icon: res.data.icon,
                };
                // if (store.coverage) {
                (storeData.tree as any).append(treeData, storeData.treeDefaultKey);
                // 设为树菜单选中状态
                setTimeout(() => {
                    (storeData.tree as any).setCurrentKey(treeData.id);
                    (storeData.tree as any).setChecked(treeData.id, true);
                }, 300);

                store.tool.toolDialog = true;
                // } else {
                //     toTreeDatas(storeData.treeData, treeData);
                // }

                // 添加map数据
                storages.set({
                    coverageMap: {
                        key: objData.id,
                        value: objData,
                    },
                });
            });
        })
        .on(mars.mars3d.EventType.drawStart, (e: any) => {
            console.log("开始绘制");
            let objData = e.graphic;
            objData.options.icon = storeData.drawIcon;
            store.coverage = false;
            store.tool.toolDialog = false;
            // console.log(objData.style);
            // console.log(graphicLayer.graphics);
        })
        .on(mars.mars3d.EventType.drawAddPoint, (e: any) => {
            // console.log("绘制过程中增加了点", e);
            let objData = e.graphic;
            // console.log(objData);
        })
        .on(mars.mars3d.EventType.drawRemovePoint, (e: any) => {
            // console.log("绘制过程中删除了点", e);
        })
        .on(mars.mars3d.EventType.editMovePoint, (e: any) => {
            console.log("编辑修改了点", e.graphic);
            // compute(e.graphic);
            // 判断当前编辑对象是否选择显示长度或面积
            // if ((global.graphic as any).options.isLength) {
            //     compute(global.graphic);
            // }
        })
        .on(mars.mars3d.EventType.drawMouseMove, (e: any) => {
            // console.log("正在移动鼠标中，绘制过程中鼠标移动了点 标绘事件", e);
            let objData = e.graphic;
            // console.log(objData);
            // compute(objData);
        })
        .on(mars.mars3d.EventType.editRemovePoint, (e: any) => {
            // console.log("删除了点", e);
        })
        .on(mars.mars3d.EventType.editStart, (e: any) => {
            // console.log("开始编辑 ", e);
            let objData = e.graphic;
            // 关闭图层菜单
            store.coverage = false;
            // 设置树菜单当前对象为选中状态
            (storeData.tree as any).setCurrentKey(objData.id, true);
        })
        .on(mars.mars3d.EventType.rightClick, (e: any) => {
            // 绑定右键事件
            console.log("右键事件 ", e.graphic);
            if (e.graphic.options.attr.from == "farmboss") {
                return false;
            }
            e.stopPropagation();
            // 判断是否在编辑状态
            if (store.property) {
                return false;
            } else {
                // 右键菜单参数赋值
                store.contextMenu.X = e.position.x;
                store.contextMenu.Y = e.position.y;
                store.contextMenu.type = "draw";
                store.contextMenu.isShow = true;
                // 传递右键点击的标绘图层对象
                storages.set({
                    graphicId: e.graphic.id,
                });
                console.log(storages.get("graphicId"));
            }
        })
        .on(mars.mars3d.EventType.click, (e: any) => {
            // 绑定点击事件
            const graphic = e.graphic;
            // 如果右键菜单是打开状态，关闭右键菜单
            if (store.contextMenu.isShow) {
                store.contextMenu.isShow = false;
            }
            setTimeout(() => {
                if (store.coverage) {
                    (storeData.tree as any).setCurrentKey(graphic.id);
                }
            }, 300);
        })
        .on(mars.mars3d.EventType.dblClick, (e: any) => {
            // 绑定双击事件
            // console.log("双击图层对象");
        });
}
/**
 * 添加数据到图层
 */
const toTreeDatas = (treeData: any, data: any) => {
    treeData.forEach((v: any, i: any) => {
        if (v.id == data.pid) {
            v.children.unshift(data);
        } else {
            if (v.children) {
                toTreeDatas(v.children, data);
            }
        }
    });
};
// /**
//  * @function compute 计算线段长度或者对象面积
//  * @param graphic 图层对象
//  * @author scw 2024-1-31 18:04:56
//  */
// function compute(graphic: any) {
//     if (graphic.type == "polyline" || graphic.type == "curve" || graphic.type == "polylineVolume" || graphic.type == "corridor") {
//         // 计算长度
//         // || graphic.type == "road"
//         // polyline--线段    curve--曲线     polylineVolume--管道    corridor--走廊  road--道路
//         console.log(graphic);
//         // if (graphic && graphic.distance) {
//         let length = graphic.distance ? graphic.distance.toFixed(2) : 0;
//         graphic.setStyle({
//             label: {
//                 text: `${length} m`,
//                 // color: "#FF0000",
//                 // opacity: 1,
//                 // clampToGround: true,
//             },
//         });
//         // }
//     } else if (graphic.type == "wall" || graphic.type == "rectangle" || graphic.type == "polygon" || graphic.type == "regular" || graphic.type == "circle") {
//         // 计算周长和面积
//         // wall-围栏    rectangle-矩形    polygon-多边形    regular-正多边形    regular-circle
//         if (graphic.type == "wall" && graphic.classify == "wall") {
//             let area = graphic.area.toFixed(2);
//             let length = graphic.distance.toFixed(2);
//             graphic.setStyle({
//                 label: {
//                     text: `面积：${area} ㎡\n周长：${length} m`,
//                     color: "#FF0000",
//                     opacity: 1,
//                     clampToGround: true,
//                 },
//             });
//         }
//     }
// }
